﻿//流程处理人属性配置
/*
1、处理人类型为角色
2、处理人类型为其它步骤的处理人
*/
var UserProperty = {
    ActivityProperty: {
        ParticipantType: {
            'typevalue': ''
        },
        RadioJob: {
            'title': "角色",
            'control': $("<input name='patype' type='radio' value='0' checked='checked'>")
        },
        RadioActivity: {
            'title': "活动",
            'control': $("<input name='patype' type='radio' value='1'>")
        },
        DivJob: {

            'control': $("<div style='width:100%;float:left;'>")
        },
        DivActivity: {

            'control': $("<div  style='width:100%;float:left; display:none;'>")
        },
        CanSelectJob: {
            'title': "可选角色",
            'AddRefresh': true,
            'control': $("<select  multiple='multiple' class='select' style='height:200px;'>")
        },
        SelectedJob: {
            'title': "已选角色",
            'control': $("<ul style='border:1px solid #658ee4; height:198px; overflow:auto'>")
        },
        CanSelectActivity: {
            'title': "可选步骤",
            'AddRefresh': false,
            'control': $("<select id='selectCanSelectActivity' multiple='multiple' class='select'  style='height:220px;'>")
        },
        SelectedActivity: {
            'title': "已选步骤",
            'control': $("<select id='selectSelectedActivity' multiple='multiple' class='select'  style='height:220px;'>")
        }
    },

    InitialPage: function (parent, activity) {
        var me = this;
        me.parent = parent;
        me.activity = activity;
        me.InitialControl(parent);
        me.InitialData(activity);
    },

    //初始化数据
    InitialControl: function (parent) {
        var me = this;
        //按钮区
        var divButtonContainer = $("<div >");
        var divbutton = $("<div class='checkBox'>");
        divButtonContainer.append(divbutton);
        divbutton.append(me.ActivityProperty.RadioJob.control);
        divbutton.append(me.ActivityProperty.RadioJob.title);
        divbutton = $("<div class='checkBox'>");
        divButtonContainer.append(divbutton);
        divbutton.append(me.ActivityProperty.RadioActivity.control);
        divbutton.append(me.ActivityProperty.RadioActivity.title);
        parent.append(divButtonContainer);
        me.ActivityProperty.RadioActivity.control.click(function () {
            me.ActivityProperty.DivJob.control.hide();
            me.ActivityProperty.DivActivity.control.show();
        });
        me.ActivityProperty.RadioJob.control.click(function () {
            me.ActivityProperty.DivJob.control.show();
            me.ActivityProperty.DivActivity.control.hide();
        });

        me.ActivityProperty.DivJob.control.empty();
        me.ActivityProperty.DivActivity.control.empty();
        //角色区
        parent.append(me.ActivityProperty.DivJob.control);
        var btns = me.InitialDiv(me.ActivityProperty.DivJob.control, me.ActivityProperty.CanSelectJob, me.ActivityProperty.SelectedJob);
        btns.btnRight.click(function () {
            var query = me.ActivityProperty.CanSelectJob.control.find("option:selected");
            $.each(query, function (i, val) {
                $(val).remove();
                me.AppendTableRow($(val).val(), $(val).text(), "", me.ActivityProperty.SelectedJob);
            })

        });
        btns.btnLeft.click(function () {

            var query = me.ActivityProperty.SelectedJob.control.find("li");

            $.each(query, function (i, val) {
                if (me.getBackgroundColor($(val)) == '#26a0da') {
                    var participantid = $(val).find("input").val();
                    var participantname = $(val).find("span").text();
                    var option = $("<option value=" + participantid + " title=" + participantname + ">" + participantname + "</option>");
                    me.ActivityProperty.CanSelectJob.control.append(option);
                    $(val).remove();
                }
            })

        });
        //活动区
        parent.append(me.ActivityProperty.DivActivity.control);
        btns = me.InitialDiv(me.ActivityProperty.DivActivity.control, me.ActivityProperty.CanSelectActivity, me.ActivityProperty.SelectedActivity);
        btns.btnRight.click(function () {
            var query = $('#selectCanSelectActivity option:selected')
            $.each(query, function (i, val) {
                $(val).remove();
                me.ActivityProperty.SelectedActivity.control.append($(val));
            })
        });
        btns.btnLeft.click(function () {
            var query = $("#selectSelectedActivity  option:selected");
            $.each(query, function (i, val) {
                $(val).remove();
                me.ActivityProperty.CanSelectActivity.control.append($(val));
            })
        });

    },
    //添加左移右移按钮
    InitialDiv: function (container, config1, config2) {
        var me = this;
        var divCanReturn = me.InitialSelect(config1);
        var divButton = $("<div style='width:15px; float:left;margin-top:100px; font-size:12px;'>");
        var btnRight = $("<input type='button' value='右移' >");
        divButton.append(btnRight);
        var btnLeft = $("<input type='button' value='左移'  style='margin-top:5px;'>");
        divButton.append(btnLeft);
        var divReturn = me.InitialSelect(config2);
        container.append(divCanReturn);
        container.append(divButton);
        container.append(divReturn);
        return {
            btnRight: btnRight,
            btnLeft: btnLeft
        };
    },
    //初始化控件数据
    InitialData: function (activity) {
        var me = this;
        if (activity.ActivityProperty != undefined && activity.ActivityProperty.activityParticipants && activity.ActivityProperty.activityParticipants.length > 0 &&
         activity.ActivityProperty.activityParticipants[0].type) {
            me.ActivityProperty.ParticipantType.typevalue = activity.ActivityProperty.activityParticipants[0].type;
            if (me.ActivityProperty.ParticipantType.typevalue == "2") {
                me.SetCheck(me.ActivityProperty.RadioActivity.control);
              
            }
            else {
                me.SetCheck(me.ActivityProperty.RadioJob.control);
            }
        }
        else {
            me.ActivityProperty.ParticipantType.typevalue = "0";
            me.SetCheck(me.ActivityProperty.RadioJob.control);
        }

        me.InitialJobData(activity);
        me.InitialActivityData(activity);
    },

    InitialJobData: function (activity) {
        var me = this;
        var selectedJob = $("#selectSelectedJob option");
        //SqlHelper.GetAllJob();
        $.each(SqlHelper.JobList, function (i, val) {
            if (activity.ActivityProperty != undefined && activity.ActivityProperty.activityParticipants != undefined) {
                var j = 0;
                var bSelected = false;
                for (h = 0; h < activity.ActivityProperty.activityParticipants.length; h++) {
                    if (val.id == activity.ActivityProperty.activityParticipants[h].participantId)
                    { j = h; bSelected = true; break; }
                }
                if (bSelected) {
                    me.AppendTableRow(val.id, val.jobName, activity.ActivityProperty.activityParticipants[j].type, me.ActivityProperty.SelectedJob);
                }
                else {
                    var option = $("<option value=" + val.id + " title=" + val.jobName + ">" + val.jobName + "</option>");
                    me.ActivityProperty.CanSelectJob.control.append(option);
                }
            }
            else {
                var option = $("<option value=" + val.id + " title=" + val.jobName + ">" + val.jobName + "</option>");
                me.ActivityProperty.CanSelectJob.control.append(option);
            }
        });
    },

    InitialActivityData: function (activity) {
        var me = this;
        $.each(activityHelper.Activitys, function (i, val) { val.LoopCount = 0; })
        activityHelper.GetReturnActivitys(activityHelper.DraftingActivity);
        //me.activity.GetReturnActivitys(true, me.activity.id);
        $.each(me.activity.Preactivity, function (i, val) {
            if (activity.ActivityProperty != undefined && activity.ActivityProperty.activityParticipants != undefined) {
                var j = 0;
                var bSelected = false;
                for (h = 0; h < activity.ActivityProperty.activityParticipants.length; h++) {
                    if (val.id == activity.ActivityProperty.activityParticipants[h].participantId)
                    { j = h; bSelected = true; break; }
                }
                if (bSelected) {
                    var option = $("<option value=" + val.id + "  title=" + val.ActivityName + ">" + val.ActivityName + "</option>");
                    me.ActivityProperty.SelectedActivity.control.append(option);
                }
                else {
                    var option = $("<option value=" + val.id + "  title=" + val.ActivityName + ">" + val.ActivityName + "</option>");
                    me.ActivityProperty.CanSelectActivity.control.append(option);
                }
            }
            else {
                var option = $("<option value=" + val.id + "  title=" + val.ActivityName + ">" + val.ActivityName + "</option>");
                me.ActivityProperty.CanSelectActivity.control.append(option);
            }
        });
    },

    SaveData: function (activity) {
        var me = this;

        if (me.ActivityProperty.RadioActivity.control.is(":checked")) {
            me.SaveActivityData(activity);
            activity.ActivityProperty.type = me.ActivityProperty.RadioActivity.control.val();
        }
        else {
            me.SaveJobData(activity);
            activity.ActivityProperty.type = me.ActivityProperty.RadioJob.control.val();
        }
    },

    SaveJobData: function (activity) {
        var me = this;
        activity.ActivityProperty.activityParticipants = new Array();
        var query = me.ActivityProperty.SelectedJob.control.find("li");

        $.each(query, function (i, val) {
            var participantid = $(val).find("input").val();
            var participanttype = $(val).find("select").val();
            activity.ActivityProperty.activityParticipants[activity.ActivityProperty.activityParticipants.length] = me.InitialParticipant(participanttype, participantid);
        })
    },

    SaveActivityData: function (activity) {
        var me = this;
        activity.ActivityProperty.activityParticipants = new Array();
        var query = me.ActivityProperty.SelectedActivity.control.find("option");
        $.each(query, function (i, val) {
            activity.ActivityProperty.activityParticipants[activity.ActivityProperty.activityParticipants.length] = me.InitialParticipant(2, $(val).val());
        })
    },

    InitialParticipant: function (participantType, participantId) {
        var me = this;
        var participant = new Object();

        participant.guid = me.activity.guid;
        participant.type = participantType;
        participant.participantId = participantId;
        participant.isDel = 0;
        return participant;
    },

    InitialRadioDiv: function () {
        var divRadio = $("<div style='width:100%;float:center;margin-top:0px;'>");

        var me = this;

        //角色
        //var rbJob = $("<input id='rbJob' style='float:left;margin-left:25px;' type='radio' value='0' checked='checked'>");
        var rbJob = me.ActivityProperty.RadioJob.control;
        var spanJob = $("<span style='float:left;'>角色</span>");
        //流程步骤
        //var rbActivity = $("<input id='rbActivity' style='float:left;margin-left:20px;' type='radio' value='1'>");
        var rbActivity = me.ActivityProperty.RadioActivity.control;
        var spanActivity = $("<span style='float:left;'>流程步骤</span>");
        divRadio.append(rbJob);
        divRadio.append(spanJob);
        divRadio.append(rbActivity);
        divRadio.append(spanActivity);

        rbJob.click(function () {
            me.JobSelect();
        });
        rbActivity.click(function () {
            me.ActivitySelect();
        });

        return divRadio;
    },

    AppendTableRow: function (tbvalue, tbname, tbtype, Prop) {
        var me = this;

        var li = $("<li style='height:20px;padding-left:5px; width:240px; line-height:20px;    font-size:12px;'>");
        li.append($("<input style='display:none;' name='participantid' value='" + tbvalue + "' />"));
        var span = $("<span style='float:left;  background-color:transparent;'>" + tbname + "</span>")
        li.append(span);
        var select = $("<select style='float:right; width:70px; background-color:transparent; margin-right:5px;'><option value='0' selected='selected'>私有</option><option value='1'>全局</option></select>")
        li.append(select);
        select.val(tbtype);
        Prop.control.append(li);

        li.bind({
            "click": function (e) {
                if (e.button == 0) {
                    if (!e.ctrlKey) {
                        Prop.control.find("li").css("background-color", "white");
                    }
                    if (me.getBackgroundColor(li) == '#26a0da')
                        li.css("background-color", "white");
                    else
                        li.css("background-color", "#26a0da");
                }
            }, "mouseenter": function () {
                if (me.getBackgroundColor(li) != '#26a0da')
                    li.css("background-color", "#e8e8e8");
            }, "mouseleave": function () {
                if (me.getBackgroundColor(li) != '#26a0da')
                    li.css("background-color", "white");
            }

        });
    },

    SetCheck: function (check) {
        check.click();
    },

    InitialSelect: function (ReturnProp) {
        var me = this;
        ReturnProp.control.empty()
        var div = $("<div style=' width:250px; float:left; margin-left:25px;margin-right:25px;'>");

        if (ReturnProp.AddRefresh) {
            var container = $("<div style='width:250px; float:left; height:20px; line-height:20px; text-align:center;font-size:12px;font-weight:bold;'>");
            div.append(container);
            var title = "<div style='float:left; margin-left:70px;'> <h3>" + ReturnProp.title + "</h3></div>"
            container.append(title);
            var divImg = $("<div style='float:left; cursor:pointer; margin-left:5px;' title='刷新'>");
            var img = $("<img>").attr("src", "/assets/js/workflow/images/toolbar/Images/refresh.png").css({ "width": "18px", "height": "18px" });
            divImg.append(img);
            container.append(divImg);
            img.click(function () {
                SqlHelper.GetAllJob();
                me.ActivityProperty.CanSelectJob.control.html("");
                me.ActivityProperty.SelectedJob.control.html("");
                me.InitialData(me.activity);

            });

        }
        else
            div.append($("<div style='width:250px; float:left; height:20px; line-height:20px; text-align:center;font-size:12px;font-weight:bold;'><h3>" + ReturnProp.title + "</h3></div>"));
        var divList = $("<div style=' width:250px; text-align:center; float:left;'>");
        div.append(divList);
        divList.append(ReturnProp.control);
        return div;
    },
    //获取div的背景色
    /*
    不同浏览器获得背景色的表线形式不同
    这里将他们都格式化为 #ffffff 的格式
    */
    getBackgroundColor: function (control) {
        var rgb = control.css('background-color');
        if (rgb.indexOf('#') != -1 || rgb == 'transparent' || rgb == 'white')
            return rgb;
        //rgb(232, 232, 232)
        rgb = rgb.split('(')[1];
        rgb = rgb.substring(0, rgb.length - 1);
        rgb = rgb.split(',');


        function hex(x) { return ("0" + parseInt(x).toString(16)).slice(-2); }
        return rgb = "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
    }

}